package middleware

import (
	"github.com/gin-gonic/gin"
	"go_blok/server/model/resp"
	"go_blok/server/utils/jwt"
)

func CheckTokenWhetherBlack() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 从请求头获取token
		token := c.Request.Header.Get("token")
		// 解析token
		claims, err := jwt.ParseToken(token)
		if token == "" || err != nil {
			resp.FailWithMsg("未登录", c)
			c.Abort()
			return
		}
		// 判断是否在黑名单
		if jwt.CheckTokenBlack(token) {
			resp.FailWithMsg("token已失效", c)
			c.Abort()
			return
		}
		// 将claims的信息存储到上下文
		c.Set("claims", claims)
		c.Next()
	}
}
